package com.cml.springcloud.service.impl;

import com.cml.springcloud.dao.OrderDao;
import com.cml.springcloud.domain.Order;
import com.cml.springcloud.service.AccountService;
import com.cml.springcloud.service.OrderService;
import com.cml.springcloud.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author cml
 * @create 2021/4/29 14:16
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderDao orderDao;
    @Resource
    private AccountService accountService;
    @Resource
    private StorageService storageService;

    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public void create(Order order) {
        log.info("------------>开始创建订单");
        orderDao.create(order);

        log.info("------------>订单微服务开始调用库存 ，做扣减Count");
        storageService.decrease(order.getProductId(),order.getCount());
        log.info("------------>订单微服务开始调用库存 ，做扣减End");

        log.info("------------>订单微服务开始调用账户 ，做扣减Money");
        accountService.decrease(order.getUserId(),order.getMoney());
        log.info("------------>订单微服务开始调用账户 ，做扣减End");

        log.info("---->修改订单状态");
        orderDao.update(order.getUserId(),0);
        log.info("---->订单状态修改成功");

        log.info("---->下订单结束");
    }

    @Override
    public void update(Long userId, Integer status) {

    }
}
